package oop.day03;

public class PolymorphicDemo
{
    public static void main(String[] args)
    {
        /*
        Dog dog = new Dog("1",1,"a");
        // 向上转型
        Animal animal = new Dog("Animal",2,"c");
        Swim swim = new Dog("swim",1,"rr");
        // 向下转型
        swim = (Dog)swim;
        System.out.println(((Dog) swim).age);
         */
        Animal animal = new Dog("Animal",2,"c");
        animal.eat();  // 调用的方法是子类是实现过的
        /*
        Master master = new Master();
        Dog dog = new Dog("a",1,"c");
        Chick chick = new Chick("t",1,"c");
        Fish fish = new Fish("f",1,"cc");
        master.feed(dog);
        // 系统自动向上转型
        master.feed(chick);
        master.feed(fish);
        */

        Animal[] animals = new Animal[3];
        animals[0] = new Dog("DouDou",1,"green");
        animals[1] = new Fish("little Gold",1,"blue");
        animals[2] = new Chick("Ga Ga Ga",3,"red");
        for (int i = 0; i < animals.length; i++)
        {
            System.out.println(animals[i].getClass());
        }
        for (int i = 0; i < animals.length; i++)
        {
            Animal animal1 = animals[i];
            System.out.println( animal1.name + " " + animal1.age + "  " + animal1.color);
            animal1.eat();   // 只能访问animal有的方法和属性
            animal1.drink();
            if(animal1 instanceof Dog)
            {
                animal1 = (Dog)animal1;
                animal1.eat();
                animal1.drink();
            }
            if(animal1 instanceof Chick)
            {
                animal1 = (Chick)animal1;
                animal1.eat();
                animal1.drink();
            }
            if(animal1 instanceof Swim)
            {
                // 能用父则用父 因为父类的囊括范围大
                Swim swim= (Swim)animal1;
                swim.swim();
            }

            for (int j = 0; j < animals.length; j++)
            {
                System.out.println(animals[i].getClass());
            }


        }



        /*

        // 向下转型
        // 成功的条件        引用所指向的对象  就是该类型 或 实现了某个结构 或 继承了某个类   如dog 可以转
        // 想强转 一定要先判断 instanceof
       Animal o = new Dog("D",11,"3");
        if (o instanceof Fish)
        {
            o = (Fish)o;
        }else if(o instanceof Chick)
        {
            o = (Chick)o;
        }else
        {
            Dog dog = (Dog)o;
            o = (Dog)o;

            o.drink();
            o.eat();
            ((Dog) o).swim();
            dog.swim();
        }

         */
    }
}
